package com.demo.layload.myapplication;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ListViewCompat;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.app.ProgressDialog;

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private String   WIP ;
    private String myWifiName ;
    private String myIp;
    private String myMac;
    private TextView connectWifiInfo;
    private ListView arpLv;
    private ArrayList<String> arpList = new ArrayList<String>();
    private TextView mHello;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // connectWifiInfo = (TextView) findViewById(R.id.connectWifiInfo);
        // arpLv = (ListView) findViewById(R.id.arplist);

        // getNetworkInfo();
        // readArp();

        setContentView(R.layout.activity_main);
        mHello = (TextView) findViewById(R.id.hello);

        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage("正在扫描");
        dialog.setCanceledOnTouchOutside(false);
        IpScanner ipScanner = new IpScanner();
        ipScanner.setOnScanListener(new IpScanner.OnScanListener() {
            @Override
            public void scan(Map<String, String> resultMap) {
                dialog.dismiss();
                mHello.setText(resultMap.toString());
            }
        });
        ipScanner.startScan();
        dialog.show();
    }

    // @Override
    // public void onClick(View v) {
    //     if(v.getId()==R.id.btnRefresh){
    //         getNetworkInfo();
    //         readArp();
    //     }
    // }

    // private void readArp() {
    //     try {
    //         BufferedReader br = new BufferedReader(
    //                 new FileReader("/proc/net/arp"));
    //         String line = "";
    //         String ip = "";
    //         String flag = "";
    //         String mac = "";

    //         while ((line = br.readLine()) != null) {
    //             try {
    //                 line = line.trim();
    //                 if (line.length() < 63) continue;
    //                 if (line.toUpperCase(Locale.US).contains("IP")) continue;
    //                 ip = line.substring(0, 17).trim();
    //                 flag = line.substring(29, 32).trim();
    //                 mac = line.substring(41, 63).trim();
    //                 if (mac.contains("00:00:00:00:00:00")) continue;
    //                 Log.e("scanner", "readArp: mac= "+mac+" ; ip= "+ip+" ;flag= "+flag);
    //                 String arp = "挨批：ip: "+ip+" | "+"麦克地址，mac: "+mac+" | "+"flag: "+flag;


    //                 arpList.add(arp);
    //             } catch (Exception e) {
    //                 continue;
    //             }
    //         }
    //         br.close();

    //         ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    //                 MainActivity.this, android.R.layout.simple_list_item_1, arpList);
    //         arpLv.setAdapter(adapter);


    //     } catch(Exception e) {
    //     }




    // }

    // private void getNetworkInfo() {
    //     try {
    //         WifiManager wm = null;
    //         try {
    //             wm = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    //         } catch (Exception e) {
    //             wm = null;
    //         }
    //         if (wm != null && wm.isWifiEnabled()) {
    //             WifiInfo wifi = wm.getConnectionInfo();
    //             if (wifi.getRssi() != -200) {
    //                 myIp = getWifiIPAddress(wifi.getIpAddress());
    //             }
    //             myWifiName = wifi.getSSID(); //获取被连接网络的名称
    //             myMac =  wifi.getBSSID(); //获取被连接网络的mac地址
    //             String str = "WIFI: "+myWifiName+"\n"+"WiFiIP: "+myIp+"\n"+"MAC: "+myMac;
    //             connectWifiInfo.setText(str);
    //             discover(myIp);// 发送arp请求
    //         }
    //     } catch (Exception e) {
    //         e.getMessage();
    //     }
    // }



    // private void discover(String ip) {
    //     String newip = "";
    //     if (!ip.equals("")) {
    //         String ipseg = ip.substring(0, ip.lastIndexOf(".")+1);
    //         for (int i=2; i<255; i++) {
    //             newip = ipseg+String.valueOf(i);
    //             if (newip.equals(ip)) continue;
    //             Thread ut = new UDPThread(newip);
    //             ut.start();
    //         }
    //     }
    // }

    // private String getWifiIPAddress(int ipaddr) {
    //     String ip = "";
    //     if (ipaddr == 0) return ip;
    //     byte[] addressBytes = {(byte)(0xff & ipaddr), (byte)(0xff & (ipaddr >> 8)),
    //             (byte)(0xff & (ipaddr >> 16)), (byte)(0xff & (ipaddr >> 24))};
    //     try {
    //         ip = InetAddress.getByAddress(addressBytes).toString();
    //         if (ip.length() > 1) {
    //             ip = ip.substring(1, ip.length());
    //         } else {
    //             ip = "";
    //         }
    //     } catch (UnknownHostException e) {
    //         ip = "";
    //     } catch (Exception e) {
    //         ip = "";
    //     }
    //     return ip;
    // }


}
